home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1993, 1996, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
-
- /* fineClip.c - render a bitmap font string using X Window System fonts;
- * adjust the viewport so the text is not prematurely clipped.
- *
- * Escape key - exit the program
- */
-
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <stdio.h>
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid drawScene( GLvoid );
- GLvoid animate( GLvoid );
- GLvoid visibility( int );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid keyboard( GLubyte, GLint, GLint );
-
- int getBitmapStringLength( void *, char * );
-
- void printHelp( char * );
-
- /* Global Definitions */
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- /* Global Variables */
-
- static GLdouble left, right, bottom, top;
- static GLint strLength;
- static void *fixedFont, *strokeFont;
- static char *charStr = "A text string.";
-
- GLvoid
- main( int argc, char *argv[] )
- {
- GLsizei width, height;
-
- glutInit( &argc, argv );
-
- width = glutGet( GLUT_SCREEN_WIDTH );
- height = glutGet( GLUT_SCREEN_HEIGHT );
- glutInitWindowPosition( (width / 2), height / 4 );
- glutInitWindowSize( (width / 2) - 4, height / 2 );
- glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutIdleFunc( animate );
- glutVisibilityFunc( visibility );
- glutKeyboardFunc( keyboard );
- glutReshapeFunc( reshape );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
- void
- printHelp( char *progname )
- {
- fprintf(stdout,
- "\n%s - render a bitmap font string; adjust the viewport "
- "so the text is\nnot prematurely clipped.\n\n"
- "Escape Key - exit the program\n\n",
- progname);
- }
-
- GLvoid
- initgfx( GLvoid )
- {
- GLdouble aspect;
-
- glClearColor( 0.0, 0.0, 0.0, 1.0 );
- glShadeModel( GL_FLAT );
-
- /* Set up two fonts to use for rendering */
- fixedFont = GLUT_BITMAP_TIMES_ROMAN_24;
- strokeFont = GLUT_STROKE_ROMAN;
-
- strLength = getBitmapStringLength( fixedFont, charStr );
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case KEY_ESC: /* Exit whenever the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- GLdouble aspect;
-
- glViewport( -strLength, 0, width + strLength, height );
-
- aspect = (GLdouble) (width + strLength) / (GLdouble) height;
- if ( aspect < 1.0 ) {
- left = -2.0;
- right = 2.0;
- bottom = -2.0 * ( 1.0 / aspect );
- top = 2.0 * ( 1.0 / aspect );
- } else {
- left = -2.0 * aspect;
- right = 2.0 * aspect;
- bottom = -2.0;
- top = 2.0;
- }
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- glOrtho( left, right, bottom, top, -1.0, 1.0 );
- glMatrixMode( GL_MODELVIEW );
- }
-
- GLvoid
- animate( GLvoid )
- {
- /* Tell GLUT to redraw the scene */
- glutPostRedisplay();
- }
-
- GLvoid
- visibility( int state )
- {
- if (state == GLUT_VISIBLE) {
- glutIdleFunc( animate );
- } else {
- glutIdleFunc( NULL );
- }
- }
-
- GLvoid
- renderBitmapString( void *font, char *string )
- {
- int i;
- int len = (int) strlen(string);
- for (i = 0; i < len; i++) {
- glutBitmapCharacter(font, string[i]);
- }
- }
-
- /* Compute the length of a bitmap string ( in pixels ) */
- int
- getBitmapStringLength( void *font, char *string )
- {
- int width = 0;
-
- #if GLUT_API_VERSION >= 3
- int i, len = strlen(string);
-
- for (i = 0; i < len; i++) {
- width += glutBitmapWidth(font, string[i]);
- }
- #else
- width += strlen(string) * 9;
- #endif
- return width;
- }
-
- GLvoid
- renderStrokeString( void *font, char *string )
- {
- int i;
- int len = (int) strlen(string);
- for (i = 0; i < len; i++) {
- glutStrokeCharacter(font, string[i]);
- }
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- char str[16];
- static GLfloat whiteColor[] = { 1.0, 1.0, 1.0 };
- static GLfloat x = 3.0;
-
- glClear( GL_COLOR_BUFFER_BIT );
-
- XYaxes();
-
- /* update the x position each time we're called;
- * wrap when we go off the screen */
- x -= 0.02;
- if ( x <= left )
- x = right ;
-
- glColor3fv( whiteColor );
-
- glPushMatrix();
- /* Bitmap characters ARE NOT affected by
- * modeling transformations
- */
- glRotatef( 15.0, 0.0, 0.0, 1.0 );
- glScalef( 2.0, 2.0, 2.0 );
-
- /* Set position for the left end of the baseline for
- * our text string */
- glRasterPos2f( x, 0.0 );
- renderBitmapString( fixedFont, charStr );
-
- /* show the baseline */
- glBegin( GL_LINES );
- glVertex2f( x, 0.0 );
- glVertex2f( x+1.0, 0.0 );
- glEnd();
- glPopMatrix();
-
- glPushMatrix();
- /* Stroke characters ARE affected by
- * modeling transformations
- */
- glTranslatef( -1.0, 0.0, 0.0 );
- glRotatef( 15.0, 0.0, 0.0, 1.0 );
- glScalef(0.003, 0.003, 0.003);
- sprintf( str, "x = %5.2f", x );
- renderStrokeString( strokeFont, str );
- glPopMatrix();
-
-
- glutSwapBuffers();
- }
-